package cho09;

/**
 * Created by 春旺 on 2017/4/16.
 */
//********************************************************************
// Secret.java Java Foundations
//
// Represents a secret message that can be encrypted and decrypted.
//********************************************************************
import java.util.Random;
public class Secret implements Encryptable
{
    private String message;
    private boolean encrypted;
    private int shift;
    private Random generator;
    //-----------------------------------------------------------------
// Constructor: Stores the original message and establishes
// a value for the encryption shift.
//-----------------------------------------------------------------
    public Secret (String msg)
    {
        message = msg;
        encrypted = false;
        generator = new Random();
        shift = generator.nextInt(10) + 5;
    }
    //-----------------------------------------------------------------
// Encrypts this secret using a Caesar cipher. Has no effect if
// this secret is already encrypted.
//-----------------------------------------------------------------
    public void encrypt ()
    {
        if (!encrypted)
        {
            String masked = "";
            for (int index=0; index < message.length(); index++)
                masked = masked + (char)(message.charAt(index)+shift);
            message = masked;
            encrypted = true;
        }
    }
    //-----------------------------------------------------------------
// Decrypts and returns this secret. Has no effect if this
// secret is not currently encrypted.
//-----------------------------------------------------------------
    public String decrypt()
    {
        if (encrypted)
        {
            String unmasked = "";
            for (int index=0; index < message.length(); index++)
                unmasked = unmasked + (char)(message.charAt(index)-shift);
            message = unmasked;
            encrypted = false;
        }
        return message;
    }
    //-----------------------------------------------------------------
// Returns true if this secret is currently encrypted.
//-----------------------------------------------------------------
    public boolean isEncrypted()
    {
        return encrypted;
    }
    //-----------------------------------------------------------------
// Returns this secret (may be encrypted).
//-----------------------------------------------------------------
    public String toString()
    {
        return message;
    }
}

